﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.Analytics.BusinessLogic;
using Google.Analytics.Common;
using Google.Analytics.Model;
using Google.Analytics.Model.Queries;
using Google.Analytics.Web.Converters;
using Microsoft.Practices.Unity;
using Dimensions=Google.Analytics.Model.Queries.Dimensions;

namespace Google.Analytics.Web
{
    public class AnalyticsGrid : Repeater
    {
        #region UnityContainer

        private UnityContainer analyticsContainer;
        /// <summary>
        /// Gets the unity container to resove the 
        /// </summary>
        public UnityContainer AnalyticsContainer
        {
            get
            {
                if (analyticsContainer == null)
                {
                    analyticsContainer = (UnityContainer)Page.Application["Google.Analytics"];
                    if (analyticsContainer == null) throw new AnalyticsException(ExceptionText.InvalidUnityContainer);
                }
                return analyticsContainer;
            }
        }
        #endregion

        #region Public Properties

        private const string authKey = "authKey";

        /// <summary>
        /// Authentication details using <see cref="AuthState"/> object
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public AuthState Authentication
        {
            get { return (AuthState)ViewState[authKey]; }
            set { ViewState[authKey] = value; }
        }

        /// <summary>
        /// Gets or Sets the username
        /// </summary>
        public string AccountID { get; set; }

        /// <summary>
        /// Gets or Sets the Metrics for the query
        /// </summary>
        [TypeConverterAttribute(typeof(EnumConverter<Metrics>))]
        public Metrics[] Metrics { get; set; }

        /// <summary>
        /// Gets or Sets the dimensions for the query
        /// </summary>
        [TypeConverter(typeof(EnumConverter<Dimensions>))]
        public Dimensions[] Dimensions { get; set; }

        /// <summary>
        /// Gets or Sets the order of the query
        /// </summary>
        [TypeConverterAttribute(typeof(SortConverter))]
        public Order[] Order { get; set; }

        /// <summary>
        /// Gets or Sets the filter
        /// </summary>
        public string Filter { get; set; }

        private DateTime startDate = DateTime.Now.AddMonths(-1);
        /// <summary>
        /// Gets or Sets the start date for the query
        /// </summary>
        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        private DateTime endDate = DateTime.Now;
        /// <summary>
        /// Gets or Sets the end date for the query
        /// </summary>
        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }
        #endregion

        public override void DataBind()
        {
            //populate the graph based on the data
            var manager = AnalyticsContainer.Resolve<DataManager>();
            var auth = Authentication;
            var data = manager.GetData(ref auth, AccountID, Dimensions, Metrics, Filter,
                            Order, StartDate, EndDate);
            Authentication = auth;
            DataSource = data;
            base.DataBind();
        }
    }
}
